package com.yingda.electronicscaleadvices.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.hardware.display.DisplayManager;
import android.view.Display;
import android.view.Gravity;
import android.widget.Toast;


import com.yingda.electronicscaleadvices.MyAPP;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {

    private static long lastClickTime;

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 600) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    /**
     * 手机号码检查
     */
    public static boolean phonenumberCheck(String phoneNumber) {

        // 表达式对象
        Pattern p = Pattern.compile("^(1)[0-9]{10}$");

        // 创建 Matcher 对象
        Matcher m = p.matcher(phoneNumber);

        return m.matches();
    }

    public static PackageInfo getPackageInfo() {
        PackageInfo info = new PackageInfo();
        try {
            info = MyAPP.getInstance()
                    .getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(
                            MyAPP.getInstance().getPackageName(),
                            0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return info;
    }

    public static String getFormatYuanFromCent(double money) {
        DecimalFormat df = new DecimalFormat("##,###,##0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df.format(money);
    }

    //副屏显示toast
    // 在副屏上显示Toast消息
    public static void showOnSecondaryScreen(Context context, String message) {
        // 获取副屏的Display对象

        DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays =  displayManager.getDisplays();
        Display secondaryDisplay = null;
        for (Display display : displays) {
            if (display.getDisplayId() != Display.DEFAULT_DISPLAY) {
                secondaryDisplay = display;
                break;
            }
        }

        if (secondaryDisplay != null) {
            // 创建Toast对象
            Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);

            // 设置Toast的位置为副屏
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.setMargin(0, 0);

            // 显示Toast在副屏上
            toast.show();
        } else {
            // 如果没有找到副屏，则在主屏上显示Toast消息
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
    }

}
